package main

import (
	"context"
	"embed"
	"fmt"
	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"github.com/wailsapp/wails/v2/pkg/options/linux"
	"github.com/wailsapp/wails/v2/pkg/options/mac"
	"github.com/wailsapp/wails/v2/pkg/options/windows"
	"myproject/backend/api"
	"myproject/backend/utils"
)

//go:embed all:frontend/dist
var assets embed.FS

// wails generate module
func main() {
	db := utils.GetDB()
	seriaPort := utils.Init()
	canvasApi := api.NewCameraApi(seriaPort)
	authApi := api.NewAuthApi(db)
	customerApi := api.NewCustomerApi(db)
	fileApi := api.NewFileSystem()
	homeApi := api.NewHomeApi()

	reportApi := api.NewReportApi()
	goodsApi := api.NewGoodsApi()
	orderApi := api.NewOrderApi()
	deviceApi := api.NewDeviceApi()
	baseApi := api.NewBaseApi()
	deptApi := api.NewDeptApi()
	err := wails.Run(&options.App{
		Title:     "suporevs",
		Width:     1024,
		Height:    768,
		MinWidth:  1100, // 最小宽度
		MinHeight: 800,  // 最小高度
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		WindowStartState: options.Normal,
		CSSDragProperty:  "--wails-draggable",
		CSSDragValue:     "drag",
		// Frameless:        true,                                  // 开启无边框
		// BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 1}, // 完全透明
		OnStartup: func(ctx context.Context) {
			authApi.Ctx = ctx
			fileApi.Ctx = ctx
			reportApi.Ctx = ctx
			goodsApi.Ctx = ctx
			customerApi.Ctx = ctx
			canvasApi.Ctx = ctx
			homeApi.Ctx = ctx
			deviceApi.Ctx = ctx
			orderApi.Ctx = ctx
			baseApi.Ctx = ctx
			deptApi.Ctx = ctx
			go func() {
				if err := canvasApi.Start(ctx); err != nil {
					fmt.Printf("Error starting camera: %v", err)
					return
				}
			}()
			fmt.Println("App started")
		},
		Debug: options.Debug{
			OpenInspectorOnStartup: true, // 启动时自动打开开发者工具（打包后仍有效）
		},
		Mac: &mac.Options{
			WebviewIsTransparent: false,
			WindowIsTranslucent:  false,
		},
		// Windows 默认允许摄像头访问，但需确保驱动正常
		Windows: &windows.Options{
			WebviewIsTransparent: false,
			WindowIsTranslucent:  false,
			DisableWindowIcon:    false,
		},
		Linux: &linux.Options{
			WindowIsTranslucent: false,
		},
		Bind: []interface{}{
			authApi,
			fileApi,
			deptApi,
			goodsApi,
			customerApi,
			reportApi,
			canvasApi,
			homeApi,
			orderApi,
			deviceApi,
			baseApi,
		},
	})
	if err != nil {
		println("Error:", err.Error())
	}
}
